__author__ = 'Pierre Moermans'

import utils
from pyfacture import models
import datetime
import calendar

DAYS_AS_TEXT = ['other','other','other','other','other','saturday','sunday']

def buildDaysForTimesheet(days, holidays):
    nb_days = utils.getDaysInMonth(days[0].date)
    first_day_of_month = datetime.date(days[0].date.year, days[0].date.month, 1)
    last_day_of_month = datetime.date(days[0].date.year, days[0].date.month, nb_days)
    one_day_delta = datetime.timedelta(1)

    for day in days:
        day.part_of_invoice = True
        day.weekday = DAYS_AS_TEXT[calendar.weekday(day.date.year,day.date.month,day.date.day)]
        #day.date = day.date.strftime('%d/%m')

    first_day_day_of_week = calendar.weekday(first_day_of_month.year,first_day_of_month.month,1)
    if first_day_day_of_week < 6:
        for i in range(1,  first_day_day_of_week + 2):
            days = [__createDay(days,first_day_of_month - i * one_day_delta,False)] + days

    last_day_day_of_week = calendar.weekday(last_day_of_month.year,last_day_of_month.month,last_day_of_month.day)
    if last_day_day_of_week < 6:
        for i in range(1, 6 - last_day_day_of_week):
            days = days + [__createDay(days,last_day_of_month + i * one_day_delta,False)]
    else:
        for i in range(1, 7):
            days = days + [__createDay(days,last_day_of_month + i * one_day_delta,False)]

    return days
    

def buildDaysForMonth(day_in_month, holidays):
    nb_days = utils.getDaysInMonth(day_in_month)
    first_day_of_month = datetime.date(day_in_month.year, day_in_month.month, 1)
    last_day_of_month = datetime.date(day_in_month.year, day_in_month.month, nb_days)
    one_day_delta = datetime.timedelta(1)
    days = []
    for i in range(0,nb_days):
        days = days + [__createDay(days,first_day_of_month + i * one_day_delta,True)]

    first_day_day_of_week = calendar.weekday(first_day_of_month.year,first_day_of_month.month,1)

    if first_day_day_of_week < 6:
        for i in range(1,  first_day_day_of_week + 2):
            days = [__createDay(days,first_day_of_month - i * one_day_delta,False)] + days

    last_day_day_of_week = calendar.weekday(last_day_of_month.year,last_day_of_month.month,last_day_of_month.day)
    if last_day_day_of_week < 6:
        for i in range(1, 6 - last_day_day_of_week):
            days = days + [__createDay(days,last_day_of_month + i * one_day_delta,False)]
    else:
        for i in range(1, 7):
            days = days + [__createDay(days,last_day_of_month + i * one_day_delta,False)]
        
    return days;


def __createDay(days,date,part_of_invoice):
    day = models.Day()
    day.date = date #date.strftime('%d/%m')
    day.part_of_invoice = part_of_invoice
    day.weekday = DAYS_AS_TEXT[calendar.weekday(date.year,date.month,date.day)]
    if part_of_invoice:
        if calendar.weekday(date.year,date.month,date.day) < 5:
            day.worked_hours = 8
        else:
            day.worked_hours = 0
    return day